package com.spdd.mt.biz.manager.feishu;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class RobotSign {
    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
        String secret = "demo";
        int timestamp = 1599360473;
        System.out.printf("sign: %s", genSign(secret, timestamp));
    }

    public static String genSign(String secret, long timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
        // 把timestamp+"\n"+密钥当做签名字符串
        String stringToSign = timestamp + "\n" + secret;
        // 使用HmacSHA256算法计算签名
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(new byte[]{});
        return new String(Base64.encodeBase64(signData));
    }
}